// ----------------------------------
// RSDK Project: Sonic CD's scrapped rounds
// Script Description: Plane Sw V Object
// Script Author: Twanvanb1
// ----------------------------------

// ========================
// Aliases
// ========================

#alias object.value0 : object.extendTop
#alias object.value1 : object.extendBottom
#alias object.value2 : object.planeRD
#alias object.value3 : object.planeLU
#alias object.value4 : object.drawOrderRD
#alias object.value5 : object.drawOrderLU
#alias object.value6 : object.onGround

#alias object.Type : Player.Type

// Gravity
#alias 0	:	GRAVITY_GROUND
#alias 1	:	GRAVITY_AIR

// ========================
// subs
// ========================

sub ObjectPlayerInteraction
	CheckEqual(object.onGround, false)
	TempValue0 = checkResult
	CheckEqual(player.gravity, GRAVITY_GROUND)
	TempValue0 |= checkResult
	
	if TempValue0 == true
		getBit(TempValue1, object.propertyValue, 6)
		if TempValue1 == false
			PlayerObjectCollision(C_TOUCH, -12, object.extendTop, 12, object.extendBottom)
			if checkResult == true
				if player.XVelocity > 0
					player.collisionPlane = object.planeLU
					Player.DrawOrder = object.drawOrderLU
				else
					player.collisionPlane = object.planeRD
					Player.DrawOrder = object.drawOrderRD
				end if
			end if
		else
			PlayerObjectCollision(C_TOUCH, object.extendTop, -12, object.extendBottom, 12)
			if checkResult == true
				if player.YVelocity > 0
					player.collisionPlane = object.planeLU
					Player.DrawOrder = object.drawOrderLU
				else
					player.collisionPlane = object.planeRD
					Player.DrawOrder = object.drawOrderRD
				end if
			end if
		end if
	end if
end sub

sub ObjectDraw
	// Only draw if the player has debug mode turned on
	if Player[0].Type == TypeName[Debug Mode]
		TempValue1 = object.propertyValue
		TempValue1 &= 3
		TempValue1 += 2
		TempValue0 = 1
		TempValue0 <<= TempValue1
		TempValue1 = TempValue0
		TempValue0 <<= 19
		GetBit(TempValue2, object.propertyValue, 7)
		TempValue3 = object.propertyValue
		TempValue3 >>= 2
		TempValue3 &= 15

		GetBit(TempValue5, object.propertyValue, 6)
		if TempValue5 == false

			FlipSign(TempValue0)
			TempValue0 += object.ypos
			TempValue0 += 0x80000

			TempValue4 = 0
			while TempValue4 < TempValue1
				if TempValue2 == true
					DrawSpriteXY(20, object.xpos, TempValue0)
				end if
				DrawSpriteXY(TempValue3, object.xpos, TempValue0)
				TempValue0 += 0x100000
				TempValue4++
			loop

			if TempValue1 == 0
				DrawSprite(TempValue3)
			end if

		else
			FlipSign(TempValue0)
			TempValue0 += object.xpos
			TempValue0 += 0x80000
			TempValue4 = 0
			while TempValue4 < TempValue1
				if TempValue2 == true
					DrawSpriteXY(20, TempValue0, object.ypos)
				end if
				DrawSpriteXY(TempValue3, TempValue0, object.ypos)
				TempValue0 += 0x100000
				TempValue4++
			loop

			if TempValue1 == 0
				DrawSprite(TempValue3)
			end if

		end if
	end if
end sub

sub ObjectStartup
	LoadSpriteSheet("Global/Editor.gif")
	
	TempValue0 = 0
	TempValue1 = 35
	while TempValue0 < 4
		SpriteFrame(-8, -8, 16, 16, TempValue1, 65)
		TempValue0++
		TempValue1 += 17
	loop
	
	TempValue0 = 0
	TempValue1 = 35
	while TempValue0 < 4
		SpriteFrame(-8, -8, 16, 16, TempValue1, 82)
		TempValue0++
		TempValue1 += 17
	loop

	TempValue0 = 0
	TempValue1 = 35
	while TempValue0 < 4
		SpriteFrame(-8, -8, 16, 16, TempValue1, 99)
		TempValue0++
		TempValue1 += 17
	loop

	TempValue0 = 0
	TempValue1 = 35
	while TempValue0 < 4
		SpriteFrame(-8, -8, 16, 16, TempValue1, 116)
		TempValue0++
		TempValue1 += 17
	loop

	TempValue0 = 0
	TempValue1 = 35
	while TempValue0 < 4
		SpriteFrame(-8, -8, 16, 16, TempValue1, 133)
		TempValue0++
		TempValue1 += 17
	loop
	
	SpriteFrame(-8, -8, 16, 16, 103, 65) // #20 - back

	ArrayPos0 = 32
	while ArrayPos0 < 1056
		if Object[ArrayPos0].Type == TypeName[Path Swapper]
			TempValue0 = object[arrayPos0].propertyValue
			TempValue0 &= 3
			TempValue0 += 2
			object[arrayPos0].extendTop = 1
			object[arrayPos0].extendTop <<= TempValue0
			object[arrayPos0].extendTop <<= 3
			object[arrayPos0].extendBottom = object[arrayPos0].extendTop
			FlipSign(object[arrayPos0].extendTop)
			
			GetBit(object[arrayPos0].planeRD, object[arrayPos0].propertyValue, 2)
			GetBit(object[arrayPos0].planeLU, object[arrayPos0].propertyValue, 3)
	
			GetBit(object[arrayPos0].drawOrderRD, object[arrayPos0].propertyValue, 4)
			object[arrayPos0].drawOrderRD += 4
			GetBit(object[arrayPos0].drawOrderLU, object[arrayPos0].propertyValue, 5)
			object[arrayPos0].drawOrderLU += 4
	
			GetBit(object[arrayPos0].onGround, object[arrayPos0].propertyValue, 7)
		end if
		ArrayPos0++
	loop
end sub

// ========================
// Editor subs & Aliases
// ========================

#alias 0 : VAR_ID_SIZE
#alias 1 : VAR_ID_PLANEL
#alias 2 : VAR_ID_PLANER
#alias 3 : VAR_ID_DRAWL
#alias 4 : VAR_ID_DRAWR
#alias 5 : VAR_ID_DIRECTION
#alias 6 : VAR_ID_ONGROUND


sub RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case VAR_ID_SIZE // size
			checkResult = object.propertyValue
			checkResult &= 3
			break
			
		case VAR_ID_PLANEL // collisionPlaneU
			GetBit(checkResult, object.propertyValue, 2)
			break
			
		case VAR_ID_PLANER // collisionPlaneD
			GetBit(checkResult, object.propertyValue, 3)
			break
			
		case VAR_ID_DRAWL // drawOrderU
			GetBit(checkResult, object.propertyValue, 4)
			break
			
		case VAR_ID_DRAWR // drawOrderD
			GetBit(checkResult, object.propertyValue, 5)
			break
			
		case VAR_ID_DIRECTION // direction
			GetBit(checkResult, object.propertyValue, 6)
			break
			
		case VAR_ID_ONGROUND // onGround
			GetBit(checkResult, object.propertyValue, 7)
			break
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case VAR_ID_SIZE // size
			TempValue1 = 3
			Not(TempValue1)
			object.propertyValue &= TempValue1

			TempValue0 = editor.variableValue
			TempValue0 &= 3
			object.propertyValue |= TempValue0
			break
			
		case VAR_ID_PLANEL // collisionPlaneU
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 2, checkResult)
			break
			
		case VAR_ID_PLANER // collisionPlaneD
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 3, checkResult)
			break
			
		case VAR_ID_DRAWL // drawOrderU
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 4, checkResult)
			break
			
		case VAR_ID_DRAWR // drawOrderD
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 5, checkResult)
			break
			
		case VAR_ID_DIRECTION // direction
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 6, checkResult)
			break
			
		case VAR_ID_ONGROUND // onGround
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 7, checkResult)
			break
			
		end switch
	end if
end sub


sub RSDKDraw
	TempValue1 = object.propertyValue
	TempValue1 &= 3
	TempValue1 += 2
	TempValue0 = 1
	TempValue0 <<= TempValue1
	TempValue1 = TempValue0
	TempValue0 <<= 19
	GetBit(TempValue2, object.propertyValue, 7)
	TempValue3 = object.propertyValue
	TempValue3 >>= 2
	TempValue3 &= 15

	GetBit(TempValue5, object.propertyValue, 6)
	if TempValue5 == false

		FlipSign(TempValue0)
		TempValue0 += object.ypos
		TempValue0 += 0x80000

		TempValue4 = 0
		while TempValue4 < TempValue1
			if TempValue2 == true
				TempValue5 = 20
				TempValue5 += TempValue3
				DrawSpriteXY(TempValue5, object.xpos, TempValue0)
			end if
			DrawSpriteXY(TempValue3, object.xpos, TempValue0)
			TempValue0 += 0x100000
			TempValue4++
		loop

		if TempValue1 == 0
			DrawSprite(TempValue3)
		end if

	else
		FlipSign(TempValue0)
		TempValue0 += object.xpos
		TempValue0 += 0x80000
		TempValue4 = 0
		while TempValue4 < TempValue1
			if TempValue2 == true
				TempValue5 = 20
				TempValue5 += TempValue3
				DrawSpriteXY(TempValue5, TempValue0, object.ypos)
			end if
			DrawSpriteXY(TempValue3, TempValue0, object.ypos)
			TempValue0 += 0x100000
			TempValue4++
		loop

		if TempValue1 == 0
			DrawSprite(TempValue3)
		end if

	end if
end sub


sub RSDKLoad
	LoadSpriteSheet("Global/Editor.gif")
	
	TempValue0 = 0
	TempValue1 = 35
	while TempValue0 < 4
		SpriteFrame(-8, -8, 16, 16, TempValue1, 65)
		TempValue0++
		TempValue1 += 17
	loop
	
	TempValue0 = 0
	TempValue1 = 35
	while TempValue0 < 4
		SpriteFrame(-8, -8, 16, 16, TempValue1, 82)
		TempValue0++
		TempValue1 += 17
	loop

	TempValue0 = 0
	TempValue1 = 35
	while TempValue0 < 4
		SpriteFrame(-8, -8, 16, 16, TempValue1, 99)
		TempValue0++
		TempValue1 += 17
	loop

	TempValue0 = 0
	TempValue1 = 35
	while TempValue0 < 4
		SpriteFrame(-8, -8, 16, 16, TempValue1, 116)
		TempValue0++
		TempValue1 += 17
	loop

	TempValue0 = 0
	TempValue1 = 35
	while TempValue0 < 4
		SpriteFrame(-8, -8, 16, 16, TempValue1, 133)
		TempValue0++
		TempValue1 += 17
	loop

	TempValue0 = 0
	TempValue1 = 35
	while TempValue0 < 4
		SpriteFrame(-8, -8, 16, 16, TempValue1, 150)
		TempValue0++
		TempValue1 += 17
	loop
	
	SpriteFrame(-8, -8, 16, 16, 103, 65) // #20 - back

	AddEditorVariable("size")
	SetActiveVariable("size")
	AddEnumVariable("4 Nodes", 0)
	AddEnumVariable("8 Nodes", 1)
	AddEnumVariable("16 Nodes", 2)
	AddEnumVariable("32 Nodes", 3)

	AddEditorVariable("collisionPlaneL")
	SetActiveVariable("collisionPlaneL")
	AddEnumVariable("Plane A", 0)
	AddEnumVariable("Plane B", 1)

	AddEditorVariable("collisionPlaneR")
	SetActiveVariable("collisionPlaneR")
	AddEnumVariable("Plane A", 0)
	AddEnumVariable("Plane B", 1)

	AddEditorVariable("drawOrderL")
	SetActiveVariable("drawOrderL")
	AddEnumVariable("Low Layer", 0)
	AddEnumVariable("High Layer", 1)

	AddEditorVariable("drawOrderR")
	SetActiveVariable("drawOrderR")
	AddEnumVariable("Low Layer", 0)
	AddEnumVariable("High Layer", 1)

	AddEditorVariable("subType")
	SetActiveVariable("subType")
	AddEnumVariable("vertical", 0)
	AddEnumVariable("horizontal", 1)

	AddEditorVariable("onGround")
	SetActiveVariable("onGround")
	AddEnumVariable("false", false)
	AddEnumVariable("true", true)
end sub
